﻿using MicroCloud.Entity;
using MicroCloud.Systems;
using System.Linq;
using Vinno.Brand.Pack.Core.Dtos;
using Vinno.Brand.Pack.Data;
using IContractService = Vinno.Brand.Pack.Core.IBrandService;
using SettingOutputDto = Vinno.Brand.Pack.Core.Dtos.SettingOutputDto;

namespace Vinno.Brand.Apis.Controllers
{
    public class VinnoController : BrandControllerBase
    {
        /// <summary>
        /// 获取 字典数据存储
        /// </summary>
        protected IKeyValueStore KeyValueStore => ServiceProvider.GetRequiredService<IKeyValueStore>();

        /// <summary>
        /// 获取 服务接口
        /// </summary>
        protected IContractService ContractService => ServiceProvider.GetService<IContractService>();

        /// <summary>
        /// 获取 实体仓储模型
        /// </summary>
        protected IRepository<KeyValue, long> KeyValueRepository => ServiceProvider.GetService<IRepository<KeyValue, long>>();

        /// <summary>
        /// 获取测试
        /// </summary>
        [HttpPost]
        [Description("测试")]
        public AjaxResult Test()
        {
            var a= KeyValueRepository.QueryAsNoTracking().Count();
            var b = KeyValueRepository.QueryAsNoTracking().Any();
            return AjaxResult.Success();
        }

        /// <summary>
        /// 获取基础配置信息
        /// </summary>
        [HttpPost]
        [Description("获取基础配置信息")]
        public AjaxResult GetSetting()
        {
            var key = SettingConstants.Setting.ToMD5();
            var value = KeyValueStore.GetByKey(key)?.Value as string;
            SettingOutputDto outPutDto = new SettingOutputDto();
            if (!value.IsNullOrWhiteSpace())
            {
                SettingDto dto = value.FromJsonString<SettingDto>();
                outPutDto = dto.MapTo<SettingOutputDto>();
            }
            return AjaxResult.Success(new
            {
                outPutDto.BgUrl,
                VinnoVideoImg = outPutDto.VinnoVideoImgs.FirstOrDefault()?.RequestPath,
                outPutDto.VinnoVideoUrl,
                outPutDto.VinnoDesc,
                VinnoInviteImg = outPutDto.VinnoInviteImgs.FirstOrDefault()?.RequestPath
            });
        }

        /// <summary>
        /// 获取产品轮播图信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Description("获取产品轮播图信息")]
        public AjaxResult GetProductCarousels()
        {
            var datas = ContractService.ProductCarousels
                .Select(a => new
                {
                    a.Img.RequestPath,
                    a.OrderNo,
                    a.CreatedTime
                })
                .OrderBy(a => a.OrderNo)
                .ThenByDescending(a => a.CreatedTime).ToList();

            return AjaxResult.Success(datas);
        }

        /// <summary>
        /// 获取 高光时刻/公益之星/精彩案列  资讯信息
        /// </summary>
        [HttpPost]
        public AjaxResult GetSpaceInfos(int pageIndex, int pageSize, SpaceType? type)
        {
            var source = ContractService.SpaceInfos.Where(a => a.Type == type)
                .Select(a => new
                {
                    a.Title,
                    a.SubTitle,
                    a.Url,
                    a.ThumbImg.RequestPath,
                    a.OrderNo,
                    a.CreatedTime,
                })
                .OrderBy(a => a.OrderNo).ThenByDescending(a => a.CreatedTime);
            var outputs = source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return AjaxResult.Success(outputs);
        }

        /// <summary>
        /// 获取产品信息
        /// </summary>
        [HttpPost]
        public AjaxResult GetProducts()
        {
            var source = ContractService.Products
              .OrderBy(a => a.OrderNo).ThenByDescending(a => a.CreatedTime).Select(a => new
              {
                  a.Id,
                  a.Title,
                  TitleImgPath = a.TitleImg.RequestPath,
                  VideoImgPath = a.VideoImg.RequestPath,
                  a.IsNew,
                  a.Type
              });
            var outputs = source.ToList();
            return AjaxResult.Success(outputs);
        }

        /// <summary>
        /// 获取产品详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<AjaxResult> GetProductDetail(Guid id)
        {
            var product = (await ContractService.FindProductById(id))?.Data;
            Check.NotNull(product, nameof(product));

            var productCases = ContractService.ProductCases.Where(a => a.ProductId == id)
                .OrderBy(a => a.OrderNo).ThenBy(a => a.CreatedTime).Select(a => new
                {
                    a.CaseImg.RequestPath,
                    a.CaseDesc
                }).ToList();

            return AjaxResult.Success(new
            {
                product.Title,
                product.Description,
                product.VideoUrl,
                Cases = productCases
            });

        }

        /// <summary>
        /// 提交需求
        /// </summary>
        /// <returns></returns>
        [UnitOfWork]
        [HttpPost]
        public async Task<AjaxResult> SubmitContact(SuggestInputDto dto)
        {
            Check.NotNull(dto, nameof(dto));
            List<string> ids = new();

            var result = await ContractService.CreateSuggests(dto);
            if (!result.Succeeded)
            {
                return result.ToAjaxResult();
            }

            return AjaxResult.Success();
        }
    }
}